package com.example.mytips.seeImage;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;

public class SeeImage extends AnAction {


    @Override
    public void actionPerformed(AnActionEvent e) {
        // 获取当前项目对象
        Project project = e.getProject();
        if (project == null) {
            return;
        }

        ///上下文
        DataContext context = e.getDataContext();

        //编辑器
        Editor editor = CommonDataKeys.EDITOR.getData(context);
        if (editor == null) {
            return;
        }

        // 获取选中模型对象
        SelectionModel selectionModel = editor.getSelectionModel();
        if (selectionModel.hasSelection()) {
            // 获取选中的文本
            String selectedText = selectionModel.getSelectedText();
            if (selectedText != null && !selectedText.isEmpty()) {
                // 弹出对话框显示选中的文本
                Messages.showMessageDialog(project, "Selected Text: " + selectedText, "Selected Text", Messages.getInformationIcon());
            }
        } else {
            // 没有选中文本时弹出提示
            Messages.showInfoMessage(project, "No text selected.", "No Selection");
        }
    }
}
